<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
          <div class="content">
          <vue-html5-editor
            :content="content"
            :height="400"
            @change="updateData"
          ></vue-html5-editor>
        </div>
        <!-- <el-input v-model="dataForm.content" placeholder="内容"></el-input> -->
      </el-form-item>
      <el-form-item label="发布时间" prop="releaseTime">
        <el-input
          v-model="dataForm.releaseTime"
          placeholder="发布时间"
        ></el-input>
      </el-form-item>
      <el-form-item label="发布部门" prop="department">
        <el-input
          v-model="dataForm.department"
          placeholder="发布部门"
        ></el-input>
      </el-form-item>
      <el-form-item label="发放时间" prop="paymentTime">
        <el-input
          v-model="dataForm.paymentTime"
          placeholder="发放时间"
        ></el-input>
      </el-form-item>
      <el-form-item label="受益群体" prop="person">
        <el-input v-model="dataForm.person" placeholder="受益群体"></el-input>
      </el-form-item>
      <el-form-item label="发放形式" prop="form">
        <el-input v-model="dataForm.form" placeholder="发放形式"></el-input>
      </el-form-item>
      <el-form-item label="金额" prop="money">
        <el-input v-model="dataForm.money" placeholder="金额"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
        content:'请输入并编辑内容',
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        content: "",
        releaseTime: "",
        department: "",
        paymentTime: "",
        person: "",
        form: "",
        money: ""
      },
      dataRule: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
        releaseTime: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
        department: [
          { required: true, message: "发布部门不能为空", trigger: "blur" }
        ],
        paymentTime: [
          { required: true, message: "发放时间不能为空", trigger: "blur" }
        ],
        person: [
          { required: true, message: "受益群体不能为空", trigger: "blur" }
        ],
        form: [
          { required: true, message: "发放形式不能为空", trigger: "blur" }
        ],
        money: [{ required: true, message: "金额不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
     updateData(e) {
      console.log(e)
      this.dataForm.context = e;
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/huiNongSubsidy/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.policiesRegulationsEntity.id;
              this.dataForm.name = data.policiesRegulationsEntity.name;
              this.dataForm.content = data.policiesRegulationsEntity.content;
              this.dataForm.releaseTime =
                data.policiesRegulationsEntity.releaseTime;
              this.dataForm.department =
                data.policiesRegulationsEntity.department;
              this.dataForm.paymentTime =
                data.policiesRegulationsEntity.paymentTime;
              this.dataForm.person = data.policiesRegulationsEntity.person;
              this.dataForm.form = data.policiesRegulationsEntity.form;
              this.dataForm.money = data.policiesRegulationsEntity.money;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/huinong/huiNongSubsidy/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              content: this.dataForm.content,
              releaseTime: this.dataForm.releaseTime,
              department: this.dataForm.department,
              paymentTime: this.dataForm.paymentTime,
              person: this.dataForm.person,
              form: this.dataForm.form,
              money: this.dataForm.money
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",

                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
